
package com.be.beadmin.quality.impl;

import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaQueryBuilderNew;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.quality.dto.input.InspectionTemplateMasterItemDto;
import com.be.beadmin.quality.dto.output.InspectionTemplateMasterItemEx;
import com.be.beadmin.quality.entity.InspectionTemplateMasterItem;
import com.be.beadmin.quality.entity.InspectionTemplateMasterItem_;
import com.be.beadmin.quality.mapper.InspectionTemplateMapper;
import com.be.beadmin.quality.mapper.InspectionTemplateMasterItemMapper;
import com.be.beadmin.quality.mapper.InspectionTemplateMasterItemMapperEx;
import com.be.beadmin.quality.repository.InspectionTemplateMasterItemRepository;
import com.be.beadmin.quality.repository.InspectionTemplateRepository;
import com.be.beadmin.quality.service.InspectionTemplateMasterItemService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description 服务实现
* @author ljh
* @date 2023-05-18
**/
@Service
@RequiredArgsConstructor
public class InspectionTemplateMasterItemServiceImpl implements InspectionTemplateMasterItemService {

    @Autowired
    private  InspectionTemplateMasterItemRepository inspectionTemplateMasterItemRepository;
    @Autowired
    private  InspectionTemplateMasterItemMapper inspectionTemplateMasterItemMapper;
    @Autowired
    private  InspectionTemplateMasterItemMapperEx inspectionTemplateMasterItemExMapper;

    @Autowired
    private InspectionTemplateRepository inspectionTemplateRepository;
    @Autowired
    private InspectionTemplateMapper inspectionTemplateMapper;

    @Override
    public Page<InspectionTemplateMasterItemEx> getInspectionTemplateMasterItemByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<InspectionTemplateMasterItem> page = inspectionTemplateMasterItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilderNew.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(InspectionTemplateMasterItem_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(InspectionTemplateMasterItem_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<InspectionTemplateMasterItemEx> page1 = page.map(inspectionTemplateMasterItemExMapper::toEntity);
            return page1;

    }

    @Override
    public void addInspectionTemplateMasterItem(InspectionTemplateMasterItemDto inspectionTemplateMasterItemDto) {
            InspectionTemplateMasterItem result = inspectionTemplateMasterItemRepository.findInspectionTemplateMasterItemByIdAndDelete(inspectionTemplateMasterItemDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("InspectionTemplateMasterItem不存在,请重新查询");
            }
            InspectionTemplateMasterItem inspectionTemplateMasterItem = inspectionTemplateMasterItemMapper.toEntity(inspectionTemplateMasterItemDto);
            inspectionTemplateMasterItemRepository.save(inspectionTemplateMasterItem);


    }

    @Override
    public void updateInspectionTemplateMasterItem(InspectionTemplateMasterItemDto inspectionTemplateMasterItemDto) {
            InspectionTemplateMasterItem inspectionTemplateMasterItem = inspectionTemplateMasterItemRepository.findInspectionTemplateMasterItemById(inspectionTemplateMasterItemDto.getId());
            inspectionTemplateMasterItem = inspectionTemplateMasterItemMapper.toEntity(inspectionTemplateMasterItemDto);
            inspectionTemplateMasterItemRepository.save(inspectionTemplateMasterItem);
    }


    @Override
    public void delInspectionTemplateMasterItem(String id) {
        InspectionTemplateMasterItem inspectionTemplateMasterItem = inspectionTemplateMasterItemRepository.findInspectionTemplateMasterItemById(id);
        inspectionTemplateMasterItem.setDelete(1);
        inspectionTemplateMasterItemRepository.save(inspectionTemplateMasterItem);
    }

    @Override
    public void delInspectionTemplateMasterItems (List<String> ids) { }

    @Override
    public List<InspectionTemplateMasterItemDto>getInspectionTemplateMasterItem(String inspectionTemplateId) {
        List<InspectionTemplateMasterItem> inspectionTemplateMasterItemList=inspectionTemplateMasterItemRepository.findInspectionTemplateMasterItemByInspectionTemplateIdAndDelete(inspectionTemplateId,0);
        return inspectionTemplateMasterItemMapper.toDto(inspectionTemplateMasterItemList);
    }

    @Override
    public InspectionTemplateMasterItem getInspectionTemplateMasterItemBySortId(String inspectionTemplateId, int sortId) {
        if(inspectionTemplateId==null) throw new BeadminException("未指定检验表模板id");
        if(sortId<=0) throw new BeadminException("未指定排序序号或序号不是正整数");
        InspectionTemplateMasterItem inspectionTemplateMasterItem = inspectionTemplateMasterItemRepository.findInspectionTemplateMasterItemByInspectionTemplateIdAndSortIdAndDelete(inspectionTemplateId,sortId,0);
        return inspectionTemplateMasterItem;
    }
}